//
//  HMWelcomeViewController.swift
//  MyWeibo
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage


class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupUI()
    }

    func setupUI(){
        
        view.backgroundColor = UIColor(white: 247/255.0, alpha: 1)
        //添加控件
        view.addSubview(headerImage)
        view.addSubview(messageLabel)
        
        //添加约束
        headerImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(200)
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headerImage.snp_bottom).offset(15)
            make.centerX.equalTo(headerImage)
            
        }
    }
    //在已经显示view中添加动画效果
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //已经显示后更新头像的约束
        headerImage.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(100)
        }
        
        //添加弹簧效果 using spring 是弹簧效果
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                //添加动画效果
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                       print("动画执行完毕，跳转界面")
                        
                        
                        //发出通知给代理跳转界面
                        NSNotificationCenter.defaultCenter().postNotificationName(HMChangeRootVCNotification, object: nil)
                        
                })
        }
    }
    
    
    
    //懒加载控件
    
    //懒加载头像
    private lazy var headerImage: UIImageView = {
        
        //创建一个imageView
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
       //切圆角
        imageView.layer.cornerRadius = 40
        imageView.layer.masksToBounds = true
        
        //取到model
        let viewModel = HMUserAccountViewModel.sharedViewModel
        
       if let avatarImage = HMUserAccountViewModel.sharedViewModel.account?.avatar_large{
            
            imageView.sd_setImageWithURL(NSURL(string: avatarImage), placeholderImage: UIImage(named:"avatar_default_big"))
        }
 
        return imageView
    }()
    
    
    //懒加载label
    
    private lazy var messageLabel:UILabel = {
        
       let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        label.text = "欢迎回来"
        label.alpha = 0
        
        return label
    }()
    
    
    
}
